/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.srcptit.trainning.longph.btvn1;

/**
 *
 * @author Long Pham
 */
public class ThuVien {

    public static int ucln(int a, int b) {
        while (a * b != 0) {
            if (a > b) {
                a = a % b;
            } else {
                b = b % a;
            }
        }
        return a + b;
    }

    public static boolean ngto(long n) {
        if (n <= 1) {
            return false;
        }
        int k = (int) Math.sqrt(n);
        for (int i = 2; i <= k; i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static boolean thuannghich(long n) {
        long p = 0, k = n;
        while (n > 0) {
            p = p * 10 + n % 10;
            n = n / 10;
        }
        if (p == k) {
            return true;
        }
        return false;
    }

    public static boolean ktfibonaci(long n) {
        long a = 1, b = 1, c = 0;
        while (c > n) {
            c = a + b;
            a = b;
            b = c;
            if (c == n) {
                return true;
            }
        }
        return false;
    }

    public static boolean cacchusolasonguyento(long n) {
        while (n > 0) {
            long k = (n % 10);
            if (ngto(k) == false) {
                return false;
            }
            n = n / 10;
        }
        return true;
    }

    public static boolean chusokhonggiam(long n) {
        long a = n % 10;
        n = n / 10;
        long b = n % 10;
        while (n > 0) {
            if (a < b) {
                return false;
            }
            a = b;
            n = n / 10;
            b = n % 10;
        }
        return true;
    }

    public static void sapxep(float a[], int n) {
        for (int i = 1; i < n; i++) {
            for (int j = i + 1; j <= n; j++) {
                if (a[i] > a[j]) {
                    float tmp;
                    tmp = a[i];
                    a[i] = a[j];
                    a[j] = tmp;
                }
            }
        }
    }
}
